﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using HardwareLibrary.LogLibrary;

namespace HardwareLibrary.IniLibrary
{
    public class IniHelper
    {
        private static string localFilePath;

        private static string errLogPath;

        public IniHelper()
        {
            errLogPath = Directory.GetCurrentDirectory();
            localFilePath = errLogPath + "\\config.ini";
        }
        /// <summary>
        /// 读取本地INI(什么是本地：就是当前应用程序下的INI)
        /// </summary>
        /// <param name="section">INI文件中的段落名称</param>
        /// <param name="key">INI文件中的关键字</param>
        /// <param name="def">无法读取时候时候的缺省数值</param>
        /// <param name="retVal">读取数值</param>
        /// <param name="size">数值的大小</param>
        public void readLocalIniFile(string section, string key, string def, StringBuilder retVal, int size)
        {
            readIniFile(section, key, def, retVal, size, localFilePath);
            
        }
        public List<ini_Struct> readAllLocalIniFile(List<ini_Struct> inistructs,ref bool isexist)
        {
            isexist = true;
            List<ini_Struct> inistrs = new List<ini_Struct>(inistructs.Count);
            ini_Struct inistr = new ini_Struct();
            foreach (ini_Struct inistruct in inistructs)
            {
                inistr = inistruct;
                StringBuilder sb = new StringBuilder();
                readLocalIniFile(inistruct.section, inistruct.key, inistruct.def, sb, inistruct.size);
                if (inistruct.def!=null&&sb.ToString() != inistruct.def)
                {
                    inistr.retVal = sb.ToString();
                }
                else
                {
                    inistr.retVal = "";
                    isexist = false;
                }
                inistrs.Add(inistr);
            }
            return inistrs;
        }
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">INI文件中的段落名称</param>
        /// <param name="key">INI文件中的关键字</param>
        /// <param name="def">无法读取时候时候的缺省数值</param>
        /// <param name="retVal">读取数值</param>
        /// <param name="size">数值的大小</param>
        /// <param name="filePath">INI文件的完整的路径和名称</param>
        public void readIniFile(string section, string key, string def, StringBuilder retVal, int size, string filePath)
        {
            try
            {
                Ini_Api.GetPrivateProfileString(section, key, def, retVal, size, filePath);
            }
            catch (Exception e)
            {
                LogManager.WriteLog(LogFile.Error, e.Message);
            }
        }

        public bool writeToLocalIniFile(string section, string key, string val)
        {
            return writeToIniFile(section, key, val, localFilePath);
        }

        public bool writeToIniFile(string section, string key, string val, string filePath)
        {
            try
            {
                Ini_Api.WritePrivateProfileString(section, key, val, filePath);
                return true;
            }
            catch (Exception e)
            {
                LogManager.WriteLog(LogFile.Error, e.Message);
                return false;
            }
        }
        /// <summary>
        /// INI写入默认值
        /// </summary>
        /// <param name="srtdefs"></param>
        /// <returns></returns>
        public bool writeDefaultIniFile(List<ini_Struct> inistructs)
        {
            try
            {
                foreach (ini_Struct inistruct in inistructs)
                {
                    if (!writeToLocalIniFile(inistruct.section, inistruct.key, inistruct.retVal))
                    {
                        return false;
                    }
                    
                }
                return true;
            }
            catch(Exception e)
            {
                LogManager.WriteLog(LogFile.Error, e.Message);
                return false;
            }
        }
    }
}
